<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Transformers\LunboTransformer;
use App\Transformers\TypeTransformer;
use App\Transformers\ProductTransformer;
use App\Models\Lunbo;
use App\Models\Type;
use App\Models\Product;
use DB;
class LunboController extends Controller
{
    
    //轮播
   public function index(Request $request)
   {
     
          //城市ID   如果传城市ID 那么就展示城市下的轮播  
          $id = request('city_id','');
            if(!empty($id)){
                return $this->response->collection(Lunbo::where('is_del',0)
                          ->where('city_id',$id)
                          ->orderBy('sort','asc')                
                          ->get(), new LunboTransformer());
            }else{
                           //打乱全部随机轮播然后展示不同城市
                 return $this->response->collection(Lunbo::where('is_del',0)
                                          ->orderBy(DB::raw('RAND()'),'sort','asc')
                                          ->take(5)
                                          ->get(), new LunboTransformer());
            }

        
   }
   
   
   //专家接口
   public function type(Request $request)
   {
       $city_id  = request('city_id','');
     
   
       if(!empty($city_id)){
                         $list =  \DB::table('type_info')
                            ->where('city_id',$city_id)
                            ->get();
                          // $list = \DB::select("select type.* from type where city_id = $city_id order by concat(type.path,type.id) asc");
                           
                    $list = json_decode(json_encode($list),true);
                    $data = array(
                             'data'=>$list,  
                    );
                    return $data;
           
       }else{
           
                      //如果城市ID不存在 那么进行随机查询
                      $list =  \DB::table('type_info')
                            ->orderBy(DB::raw('RAND()'))
                            ->take(5)
                            ->get();
                      //$list = \DB::select("select type.* from type where city_id = $city_id order by concat(type.path,type.id) asc");
                      
                    $list = json_decode(json_encode($list),true);
                    $data = array(
                         'data'=>$list,  
                    );
                    return $data;
                    
       }
       
     
      
   }
   
   
    //专家详情
   public  function  typeshow()
   {
       $id = request('id');//分类ID
       $list =  \DB::table('type_info')
                ->where('id',$id)
                ->first();
            $list = json_decode(json_encode($list),true);
            return $list;
   }

    //产品接口完成
    public function product(Request $request)
    {
        $city_id  = request('city_id','');
                  if(!empty($city_id)){
                          //推荐城市全部产品
                          $list =  \DB::table('product')
                                 ->select('product.id as pid','product.pro_name as pname','product.title','product.img as p_img','product.type_id','product.price','product.y_price','type.name as type_name','type.type_img')
                                 ->leftJoin('type', 'product.type_id', '=', 'type.id')
                                 ->where('product.city_id',$city_id)
                                 ->orderBy('product.is_top','desc')
                                 ->paginate(2);
                                 $list = json_decode(json_encode($list),true);
                                 return $list;
                      }else{
                          //进行随机推荐
                           $list =  \DB::table('product')
                                 ->select('product.id as pid','product.pro_name as pname','product.title','product.img as p_img','product.type_id','product.price','product.y_price','type.name as type_name')
                                 ->leftJoin('type', 'product.type_id', '=', 'type.id')
                                 ->orderBy(DB::raw('RAND()'))
                                 ->take(5)
                                 ->paginate(2);
                                 $list = json_decode(json_encode($list),true);
                                 return $list;   
                  }


    }
   
    //产品详情
    public function proinfo()
    {
        $id = request('id');
        $openid = request('openid');
        $coll = \DB::table('pro_collection')->where('pro_id',$id)->where('open_id',$openid)->where('status',0)->first();
        if(!empty($coll)){
           
            $coll = true;
        }else{
            
            $coll = false;
        }
        $list =  \DB::table('product')
                ->select('type.logo_img','product.xuzhi','product.content','product.id as pid','product.pro_name as pname','product.title','product.img as p_img','product.type_id','product.price','product.y_price','type.id as tid','type.name as tname','type.type_img as t_img','type.text')
                ->leftJoin('type', 'product.type_id', '=', 'type.id')
                ->where('product.is_del',0)
                ->where('product.id',$id)
                ->first();
        
        $pieces = explode('#',$list->text);
       
        $xuzhi = explode('#',$list->xuzhi);
        
        $xu = array();
        $array = array();
        
       foreach($pieces as $val){
              $array[] = $val;  
        }
         
        
      foreach($xuzhi as $val){
              $xu[] = $val;  
        }
        
       $xu =  json_decode(json_encode($xu),true);
         
       $you =  json_decode(json_encode($array),true);
       
       $list = json_decode(json_encode($list),true);
       $data = array(
           'data'=>$list, 
           'you'=>$you,
           'coll'=>$coll,
           'xuzhi'=>$xu   
        ) ;
      
       
       
      
       
       return $data;
    }
    
   //产品推荐完成 推荐的是项目ID下的 
   public function typeinfo($id)
   {
            $list =  \DB::table('type')
                     ->select('product.id as pid','product.pro_name as pname','product.title','product.img as p_img','product.type_id','product.price','product.y_price','type.id as tid','type.name as tname','type.type_img as t_img','type.text')
                     ->leftJoin('product', 'type.id', '=', 'product.type_id')
                     ->where('type.id',$id)
                     //->where('product.is_top',1)
                     ->limit(4)
                     ->get();
            return  $list;
       
   }
   
   
  
   
   
   //添加报名：
   public function link()
   {
        $data['name'] =   request('name','');
        $data['openid'] =   request('openId');
        $data['pro_id'] =   request('pid');
        $data['phone'] =   request('phone','');
        $data['question'] =   request('question','');
        $data['status'] =   request('status',0);
        $data['created_at'] = date('Y-m-d H:i:s');
                
        $id = \DB::table("link")->insertGetId($data);
        
        if($id){
            $msg = array(
                            'status'=>'200',
                            'message'=>'添加成功',
                    );
             return $msg;
        }else{
             $msg = array(
                            'status'=>'101',
                            'message'=>'添加失败',
                    );
                   return $msg;
        }
       
   }
   
   
   //获取openid
   public function linkshow($openid)
   {
    
       $info = \DB::table('link')->where('openid',$openid)->get();
       $info = json_decode(json_encode($info),true);
       return $info;
            
   }
}
